export const gattServer_device_init = function (_, generator) {
    generator.definitions_['include_BLEDevice'] = '#include <BLEDevice.h>';
    generator.definitions_['include_BLEUtils'] = '#include <BLEUtils.h>';
    generator.definitions_['include_BLEServer'] = '#include <BLEServer.h>';
    var value_name = generator.valueToCode(this, 'name', generator.ORDER_ATOMIC);
    var code = `BLEDevice::init(${value_name});\n`;
    return code;
};

export const gattServer_server_init = function () {
    var text_var = this.getFieldValue('var');
    var code = `BLEServer *${text_var} = BLEDevice::createServer();\n`;
    return code;
};

export const gattServer_service_init = function (_, generator) {
    var value_uuid = generator.valueToCode(this, 'UUID', generator.ORDER_ATOMIC);
    var text_var = this.getFieldValue('var');
    var text_server = this.getFieldValue('server');
    var code = `BLEService *${text_var} = ${text_server}->createService(${value_uuid});\n`;
    return code;
};

export const gattServer_char_init = function (_, generator) {
    var text_var = this.getFieldValue('var');
    var text_service = this.getFieldValue('service');
    var value_UUID = generator.valueToCode(this, 'UUID', generator.ORDER_ATOMIC);
    generator.definitions_[`zdefine_char_${text_var}`] = `BLECharacteristic *${text_var};`;
    var code = `${text_var} = ${text_service}->createCharacteristic(
        ${value_UUID},
        BLECharacteristic::PROPERTY_READ |
        BLECharacteristic::PROPERTY_WRITE |
        BLECharacteristic::PROPERTY_NOTIFY
    );\n`;
    return code;
};

export const gattServer_set_char_value = function (_, generator) {
    var value_value = generator.valueToCode(this, 'value', generator.ORDER_ATOMIC);
    var text_var = this.getFieldValue('var');
    var code = `${text_var}->setValue(${value_value});\n`;
    return code;
};

export const gattServer_char_notify = function () {
    var text_var = this.getFieldValue('char');
    var code = `${text_var}->notify();\n`;
    return code;
};

export const gattServer_start_service = function () {
    var text_var = this.getFieldValue('var');
    var code = `${text_var}->start();\n`;
    return code;
};

export const gattServer_advertising_init = function () {
    var text_var = this.getFieldValue('var');
    var text_Server = this.getFieldValue('Server');
    var code = `BLEAdvertising *${text_var} = ${text_Server}->getAdvertising();\n`;
    return code;
};

export const gattServer_start_advertising = function () {
    var text_advertising = this.getFieldValue('advertising');
    var code = `${text_advertising}->start();\n`;
    return code;
};